package thn.glf.org.personalfinancial;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import thn.glf.org.lib.DialogButton;
import thn.glf.org.lib.MessageBox;
import thn.glf.org.personalfinancial.objData.FinancialMonth;
import thn.glf.org.personalfinancial.objData.Setting;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SettingActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		TextView txtdayketso=(TextView)findViewById(R.id.txtdayketso);
		Setting setting=new Setting();
		setting.setmContext(this);
		txtdayketso.setText(setting.getList().get(0).getDay()+"");
		Button btnhuy=(Button)findViewById(R.id.btnHuy);
		Button btnThem=(Button)findViewById(R.id.btnthem);
		
		btnhuy.setOnClickListener(Huy_Click);
		btnThem.setOnClickListener(Them_Click);
		
	}
private OnClickListener Huy_Click=new OnClickListener() {
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		finish();
	}
};
private OnClickListener Them_Click=new OnClickListener() {
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Setting setting=new Setting();
		setting.setmContext(getApplicationContext());
		TextView txtdayketso=(TextView)findViewById(R.id.txtdayketso);
		int day=Integer.parseInt("0"+txtdayketso.getText().toString());
		if(checkerror()){			
			setting.setDay(day);
			setting.Update(setting);
			FinancialMonth financialMonth=new FinancialMonth();
			financialMonth.setmContext(getApplicationContext());
			if(financialMonth.CheckExist()){
				
			}else{
				Calendar currentDate = Calendar.getInstance();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
				Calendar dateketso = Calendar.getInstance();
				dateketso.set(currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), day);
				String DateStart = formatter.format(dateketso.getTime());
				dateketso.add(Calendar.MONTH, 1);
				dateketso.add(Calendar.DAY_OF_MONTH, -1);
				String DateEnd = formatter.format(dateketso.getTime());
				financialMonth.setMonthFrom(DateStart);
				financialMonth.setMonthTo(DateEnd);
				financialMonth.setMonthID(currentDate.get(Calendar.MONTH)+1);
				financialMonth.Add(financialMonth);
			}
		}else{
			return;
		}
		
		finish();
	}
};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.setting, menu);
		return true;
	}
public boolean checkerror(){
	TextView txtdayketso=(TextView)findViewById(R.id.txtdayketso);
	int day=Integer.parseInt("0"+txtdayketso.getText().toString());
	if(day==0||day>=28){
		MessageBox.Show(this, "Ngay key so khong dung", DialogButton.OK);
		return false;
	}
	return true;
}
}
